package com.cwx.service;

import com.cwx.NotFoundException;
import com.cwx.dao.TagsRepository;
import com.cwx.pojo.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class TagsServiceImpl implements TagsService{

    @Autowired
    private TagsRepository tagsRepository;

    @Transactional
    @Override
    //添加一个标签
    public Tag saveTag(Tag tag) {
        return tagsRepository.save(tag);
    }

    @Transactional
    @Override
    //根据用户查找制定标签
    public Tag getTag(String name) {
        return tagsRepository.findByName(name);
    }

    @Transactional
    @Override
    //分页查找所有
    public Page<Tag> listTag(Pageable pageable) {
        return tagsRepository.findAll(pageable);
    }

    //查找所有不用条件
    @Transactional
    @Override
    public List<Tag> listTag() {
        return tagsRepository.findAll();
    }

    @Override
    public List<Tag> listTag(String ids) {
        return tagsRepository.findAllById(convertToList(ids));
        //通过传递一个集合的参数  返回一个tag的集合 因为前端传递过来的tags是多选的
    }

    private List<Long> convertToList(String ids){
        List<Long> list = new ArrayList<>();
        if(!"".equals(ids) && ids != null){
            String[] idarray = ids.split(",");
            for (int i = 0; i < idarray.length; i++){
                if (!isNumeric(idarray[i])){
                    Tag tag = new Tag();
                    tag.setName(idarray[i]);
                    saveTag(tag);
                    list.add(new Long(getTag(idarray[i]).getId()));
                }else {
                    list.add(new Long(idarray[i]));
                }
            }
        }
        return list;
    }

    //判断字符中是不是只包含数字
    public static boolean isNumeric(String str){
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    @Transactional
    @Override
    //修改一个分类
    public Tag updateTag(Long id, Tag tag) {
        Tag t = tagsRepository.getById(id);

        if (t == null){
            throw new NotFoundException("不存在此人");
        }else {
            BeanUtils.copyProperties(tag,t);  //前面的赋值给后面的、
            return tagsRepository.save(t);
        }
    }

    @Transactional
    @Override
    //删除一个标签
    public void deleteTag(Long id) {
        tagsRepository.deleteById(id);
    }

    @Transactional
    @Override
    //根据id查找标签
    public Tag getTagById(Long id) {
        return tagsRepository.getById(id);
    }

    //根据标签所对应的博客数量 查出前相应数量的标签
    @Transactional
    @Override
    public List<Tag> listTagTop(Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC,"blogs.size");
        Pageable pageable = PageRequest.of(0,size,sort);

        return tagsRepository.findTop(pageable);
    }
}
